package com.sponge.result;

import com.sponge.constant.ErrorCode;
import com.sponge.constant.GlobalConstant;

import java.util.Date;

public class ServiceResult<T> {

    private Integer status;
    private String message;
    private Date timestamp;
    private T data;

    public ServiceResult() {
    }

    public ServiceResult(SpongeException exception){
        this.status = exception.getStatus();
        this.message = exception.getUserMessage();
        this.timestamp = new Date();
        this.data = (T) exception.getUserMessage();
    }


    public ServiceResult(Integer status, String message, Date timestamp, T data) {
        this.status = status;
        this.message = message;
        this.timestamp = timestamp;
        this.data = data;
    }

    public ServiceResult(Integer status,String message,T data){
        this(status,message,new Date(),data);
    }

    public ServiceResult(T data){
        this(ErrorCode.NO_ERROR, GlobalConstant.SUCCESS,new Date(),data);
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static ServiceResult<String> getResult(boolean update){
        if (update){
            return new ServiceResult<>(ErrorCode.NO_ERROR, GlobalConstant.SUCCESS,new Date(), GlobalConstant.SUCCESS);
        }else{
            return new ServiceResult<>(ErrorCode.OPERATE_FAIL, GlobalConstant.ERROR,new Date(), GlobalConstant.ERROR);
        }
    }
}
